<?php
include('/home/wwwroot/football.laulei.com/protected/extensions/simple_html_dom.php');
//获取北京单场信息，如果比赛已经结束，则更新比分和赔率
class GetGameInfosCommand extends CConsoleCommand  
{
    public function run($args) {
    	$issueArry=array();
        $html = file_get_html('http://trade.500.com/bjdc/');
		$select = $html->find('select[id=expect_select]', 0);
	    $option = $select->find('option', 0);
    	$issue=$option->value;
    	$issueArrTmp = explode("|",$issue);
    	array_push($issueArry, $issueArrTmp[0]);

    	$option = $select->find('option', 1);
    	$issue=$option->value;
    	$issueArrTmp = explode("|",$issue);
    	array_push($issueArry, $issueArrTmp[0]);

		$html->clear();
		unset($html);
    	/*
    	$issueArry=array();
        $html = file_get_html('http://trade.500.com/bjdc/');
		$select = $html->find('select[id=expect_select]', 0);
	    foreach($select->find('option') as $option){
	    	$issue=$option->value;
	    	$issueArrTmp = explode("|",$issue);
	    	array_push($issueArry, $issueArrTmp[0]);
	    }
		$html->clear();
		unset($html);
		*/
		/*
		$issueArry=array();
		$beginIssue='101203';//起始期号
		$endIssue='140101';//结束期号
		array_push($issueArry, $beginIssue);
		while($beginIssue!=$endIssue){
			$issuePartYear = intval(substr($beginIssue, 0, 2));
			$issuePartMonth = intval(substr($beginIssue, 2, 2));
			$issuePartDay = intval(substr($beginIssue, 4, 2));
			printf("%s\t%s\t%s\n", $issuePartYear, $issuePartMonth, $issuePartDay);
			$issuePartDay=$issuePartDay+1;
			if($issuePartDay>20){
				$issuePartDay=1;
				$issuePartMonth=$issuePartMonth+1;
			}
			if($issuePartMonth>12){
				$issuePartMonth=1;
				$issuePartYear=$issuePartYear+1;
			}

			$beginIssue=sprintf("%02d%02d%02d", $issuePartYear, $issuePartMonth, $issuePartDay);
			array_push($issueArry, $beginIssue);
			printf("%s\t%s\t%s\n", $issuePartYear, $issuePartMonth, $issuePartDay);
			printf("%s\n", $beginIssue);
		}
		*/
		foreach ($issueArry as $key => $value) {
			try{
				$urlEveryIssue='http://zc.trade.500.com/bjdc/?expect='.$value;
				printf(date("Y-m-d H:i:s") . " %s\n", $urlEveryIssue);
		    	$htmlIssue = file_get_html($urlEveryIssue);
		    	if(!isset($htmlIssue)){
		    		continue;
		    	}
				foreach($htmlIssue->find('tr[class=vs_lines]') as $game) {
					try{
						//比赛编号
						$gameid=trim($game->fid);

						//场次编号
						$gameIndex=$game->find('td', 0);
						$gameIndex=trim($gameIndex->plaintext);

						//比赛类型
						$gameType=$game->find('td[class=league]', 0);
						$gameType=trim($gameType->plaintext);
						$gameType=iconv("GB2312","utf-8//TRANSLIT//IGNORE",$gameType);

						//让球数
						$rateBall=$game->find('td', 4);
						$rateBall=trim($rateBall->plaintext);

						//主队
						$homeTeam=$game->find('td[class=tr]', 0)->find('a', 0);
						if(!isset($homeTeam)){
							$homeTeam=trim($game->find('td[class=tr]', 0)->plaintext);
						}
						else{
							$homeTeam=trim($homeTeam->title);
						}
						$homeTeam=iconv("GB2312","utf-8//TRANSLIT//IGNORE",$homeTeam);
						
						//客队
						$awayTeam=$game->find('td[class=tl]', 0)->find('a', 0);
						if(!isset($awayTeam)){
							$awayTeam=trim($game->find('td[class=tl]', 0)->plaintext);
						}
						else{
							$awayTeam=trim($awayTeam->title);
						}
						$awayTeam=iconv("GB2312","utf-8//TRANSLIT//IGNORE",$awayTeam);

						//比分
						$result=$game->find('td[class=h_br]', 0)->find('a', 0);
						if (!isset($result)){
							continue;
						}
						$result=$result->plaintext;
						$homeGoal=-1;
						$awayGoal=-1;
						if($result!='析'){
							$goalArrTmp = explode(":",$result);
							if(count($goalArrTmp)==2){
								$homeGoal=$goalArrTmp[0];
								$awayGoal=$goalArrTmp[1];
							}
						}

						//胜平负赔率
						$euroOdds3=$game->find('td', 6)->find('span', 0);
						$euroOdds3=$euroOdds3->plaintext;
						$euroOdds1=$game->find('td', 6)->find('span', 1);
						$euroOdds1=$euroOdds1->plaintext;
						$euroOdds0=$game->find('td', 6)->find('span', 2);
						$euroOdds0=$euroOdds0->plaintext;

						$gameTime='';

						printf(date("Y-m-d H:i:s") . " begin find gameid [%s]\n", $gameid);
						$gameInfoObj=GameInfo::model()->find(array('condition'=>'issue=:issue and gameindex=:gameindex','params'=>array(':issue'=>$value, ':gameindex'=>$gameIndex)));
						if (!isset($gameInfoObj)){
							printf(date("Y-m-d H:i:s") . " end find [issue=%s] [gameindex=%s], it's not set\n", $value, $gameIndex);
							$gameInfo=new GameInfo;
							$gameInfo->gameid=$gameid;
							$gameInfo->issue=$value;
							$gameInfo->gameindex=$gameIndex;
							$gameInfo->gametype=$gameType;
							$gameInfo->hometeam=$homeTeam;
							$gameInfo->awayteam=$awayTeam;
							$gameInfo->rateball=$rateBall;
							$gameInfo->homegoal=$homeGoal;
							$gameInfo->awaygoal=$awayGoal;
							//比赛时间
							/*
							$gameTime=trim($game->value);
							$gameTime=substr($gameTime,strpos($gameTime, ',endTime:\'') + strlen(',endTime:\''),16);
							*/
							$gametimeUrl=sprintf("http://odds.500.com/fenxi/shuju-%s", $gameid);
							$htmlgametime = file_get_html($gametimeUrl);
							if(isset($htmlgametime)){
								$gameTime=$htmlgametime->find("div[class=against_m]", 0);
								$gameTime=trim($gameTime->outertext);
								$gameTime=iconv("GB2312","utf-8//TRANSLIT//IGNORE",$gameTime);
								$gameTime=explode("<br />", $gameTime);
								$gameTime=$gameTime[1];
								$gameTime=str_replace("比赛时间：", "", $gameTime);
								$gameTime=explode(" ", $gameTime);
								$gameTime=$gameTime[0]." ".$gameTime[2];
								$htmlgametime->clear();
								unset($htmlgametime);
							}

							$gameInfo->gametime=$gameTime;
							$gameInfo->euroodds3=$euroOdds3;
							$gameInfo->euroodds1=$euroOdds1;
							$gameInfo->euroodds0=$euroOdds0;
							$gameInfo->writetime=date("Y-m-d H:i:s");
							$gameInfo->updatetime=date("Y-m-d H:i:s");
							$gameInfo->save();
							printf(date("Y-m-d H:i:s") . " [期号：%s] [序号：%s] [时间：%s] [类型：%s] [主队：%s] [让球：%s] [客队：%s] [主进：%s] [客进：%s] [胜：%s] [平：%s] [负：%s]\n", $value, $gameIndex, $gameTime, $gameType, $homeTeam, $rateBall, $awayTeam, $homeGoal, $awayGoal, $euroOdds3, $euroOdds1, $euroOdds0);
						}
						else{
							$gameTime=$gameInfoObj->gametime;
							$gameInfoObj->homegoal=$homeGoal;
							$gameInfoObj->awaygoal=$awayGoal;
							$gameInfoObj->euroodds3=$euroOdds3;
							$gameInfoObj->euroodds1=$euroOdds1;
							$gameInfoObj->euroodds0=$euroOdds0;
							$gameInfoObj->updatetime=date("Y-m-d H:i:s");
							if ($gameInfoObj->update()) {
								printf(date("Y-m-d H:i:s") . " [期号：%s] [序号：%s] [时间：%s] [类型：%s] [主队：%s] [让球：%s] [客队：%s] [主进：%s] [客进：%s] [胜：%s] [平：%s] [负：%s]\n", $value, $gameIndex, $gameTime, $gameType, $homeTeam, $rateBall, $awayTeam, $homeGoal, $awayGoal, $euroOdds3, $euroOdds1, $euroOdds0);
								printf(date("Y-m-d H:i:s") . " end find gameid [%s], update it!\n", $gameid);
							}
							else{
								printf(date("Y-m-d H:i:s") . " [期号：%s] [序号：%s] [时间：%s] [类型：%s] [主队：%s] [让球：%s] [客队：%s] [主进：%s] [客进：%s] [胜：%s] [平：%s] [负：%s]\n", $value, $gameIndex, $gameTime, $gameType, $homeTeam, $rateBall, $awayTeam, $homeGoal, $awayGoal, $euroOdds3, $euroOdds1, $euroOdds0);
								printf(date("Y-m-d H:i:s") . " end find gameid [%s], update fail!\n", $gameid);
							}
						}
					}
					catch(Exception $ex){

					}
				}
			}
			catch(Exception $ex){

			}
			$htmlIssue->clear();
			unset($htmlIssue);
		}
    }
}